<template>
  <div class="resume_header">
    <span>所有人事</span>
  </div>
  <div class="resume_center">

    <el-table :data="filterTableData.slice((page - 1) * limit, page * limit)" style="width: 100%" height="520px"
      size="large" @filter-change="tablesort">
      <el-table-column label="HR姓名" prop="hrName" />
      <el-table-column label="就职公司" prop="eName" sortable width="220" column-key="eName" :filters="filterENameData" />
      <el-table-column label="HR级别" prop="hrLevel" />
      <el-table-column label="HR电话" prop="hrPhone" />
      <el-table-column label="HR邮箱" prop="hrEmail" />

      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="default" placeholder="搜索" />
        </template>
        <template #default="scope">
          <el-popconfirm width="220" confirm-button-text="是" cancel-button-text="否" icon-color="#626AEF" title="是否确认操作?"
            cancel-button-type="info" @confirm="handleInitPwd(scope.$index, scope.row)">
            <template #reference>
              <el-button size="default" type="primary">初始化密码</el-button>
            </template>
          </el-popconfirm>
          <el-popconfirm width="220" confirm-button-text="是" cancel-button-text="否" icon-color="#626AEF" title="是否确认操作?"
            cancel-button-type="info" @confirm="handleDelect(scope.$index, scope.row)">
            <template #reference>
              <el-button size="default" type="danger">删除</el-button>
            </template>
          </el-popconfirm>


        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <Pagination :page="page" :limit="limit" :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange" :filterTableData="filterTableData" :pagesizes="pagesizes" />
    </div>
  </div>
</template>
<script lang='ts' setup>
import { TableColumnCtx } from 'element-plus/es/components/table/src/table-column/defaults';
import { data, elmessage } from "@/hook/hook"
import { ElDrawer, ElMessageBox } from 'element-plus'
import { computed, ref, onMounted, reactive, watch, isProxy, toRef, toRefs, onBeforeMount, unref } from 'vue'
import { useStore } from "vuex"
import { useRouter } from "vue-router"
import { adminGetALLHRs, adminDelectHR, adminInitPwd } from "@/api/adminApi"
import Pagination from "@/components/Pagination.vue"
const store = useStore()
const router = useRouter()


const hrs = store.state.hrState.hrs


// 表格数据定义
interface HR {
  hrId: string
  hrName: string
  hrPhone: string
  hrLevel: string
  hrEmail: string
  eId: string
  eName: string
}
const tableData: HR[] = reactive([])


const search = ref('')
const filterTableData = computed(() =>
  tableData.filter(
    (data) =>
      !search.value ||
      // 通过岗位名称筛选
      data.hrName.toLowerCase().indexOf(search.value.toLowerCase()) !== -1
  )
)
// 定义筛选类型
interface filterType {
  text: string,
  value: string
}



// // 公司名称筛选排序
// // :filter-method="filterEnterprise" 
// const filterEnterprise = (
//   value: string,
//   row: HR,
//   column: TableColumnCtx<HR>
// ) => {

//   return row["eName"] === value
// }
const filterENameData: filterType[] = reactive([])

var eNameArr: any = reactive([])
const tableData_1: HR[] = reactive([])
const setENameArr = (arr: any) => {
  eNameArr = arr
}
const getENameArr = () => {
  return eNameArr
}
const tablesort = (filterArr: HR) => {
  // 清除原表格数据
  tableData.splice(0, tableData.length)
  if ((typeof (filterArr.eName)) == "object") {
    setENameArr(filterArr.eName)
  }
  const eNameLength = getENameArr().length
  // 按筛选条件重新加入数据
  tableData_1.forEach((valu: any, index: any, arr_1: any) => {
    // 所有筛选重置，即最初排序
    if (eNameLength == 0) {
      if (tableData.findIndex((item: { hrId: string; }) => item.hrId == valu["hrId"]) == -1) {
        tableData.push(valu)
      }

    }


    // 只筛选公司名称
    else if (eNameLength > 0) {
      if (getENameArr().includes(valu["eName"])) {
        if (tableData.findIndex((item: { hrId: string; }) => item.hrId == valu["hrId"]) == -1) {
          tableData.push(valu)
        }
      }
    }




  })
}

// 分页器
const page = ref(1)
const pagesizes = ref([1, 2, 3, 4, 10, 20, 30, 40])
const limit = ref(10)

const handleSizeChange = (val: number) => {
  // pushTableData()
  limit.value = val
  page.value = 1

}
const handleCurrentChange = (val: number) => {
  page.value = val
  // pushTableData()
}
const handleInitPwd = (index: number, row: HR) => {
  const newData = {
    ...data(),
    initPart: "hr",
    hrId: row.hrId
  }
  adminInitPwd(newData).then((res: any) => {
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data["message"]);

    }
  })
}
const handleDelect = (index: number, row: HR) => {
  const newData = {
    ...data(),
    ...row
  }
  adminDelectHR(newData).then((res: any) => {
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
      tableData.splice(index, 1)
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else if (res.data["code"] == "501") {
      elmessage(res.data["message"], res.data["type"])
    }
    else {
      console.log(res.data["message"]);

    }
  })


}

const init = () => {
  adminGetALLHRs(data()).then((res: any) => {
    if (res.data["code"] == "200") {
      res.data.datas.forEach((value: any, index: any, self: any) => {
        tableData.push(value)
        tableData_1.push(value)

       
      })
      // console.log(hrs.length);

      // res.data["hrs"].forEach((value: any, index: any, self: any) => {
      //   filterHRData.push(value)
      // });
      res.data["es"].forEach((value: any, index: any, self: any) => {
        filterENameData.push(value)
      });
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data["message"]);

    }
  })
}


onBeforeMount(() => {
  init()

})


</script>
<style scoped>
.pagination {
  float: right;
  margin-right: 50px;
  /* right: 150px; */
}

.addJob_btn {
  margin-right: 100px;
  /* float: right;
    right: 100px; */
  /* margin-left: 700px; */
}

.resume_center {
  font-family: '微软雅黑';
  height: 800px;
  background-color: #FFF;
}

.resume_header {
  background-color: rgb(215, 215, 215);
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  justify-content: space-between;

}

a {
  text-decoration: none;
  color: #000;
}
</style>